<?php
/**
 * IG_Log
 *
 * @author mmxie
 * @copyright Copyright (c) 2009 Green Shore (http://www.iwgame.com)
 * @version $Id: Log.php 1127 2012-01-06 11:11:21Z mmxie $
 */
class IG_Log
{
    const OPERATOR = 9;

    /**
     * Zend_Log
     * @var Zend_Log
     */
    static protected $_log;

    static protected $_identity;

    /**
     * Log a message at a priority
     *
     * @param  string   $message   Message to log
     * @param  integer  $priority  Priority of message
     * @param  mixed    $extras    Extra information to log in event
     * @return Zend_Log|void
     */
    static public function log($message = null, $priority = self::OPERATOR, $extras = null)
    {
        if (!self::$_identity) {
            $auth = Zend_Auth::getInstance();
            self::$_identity = $auth->hasIdentity() ? $auth->getIdentity()->getName()
                                                    : 'guest';
            self::$_log->setEventItem('employee', self::$_identity);
        }
        if (isset(self::$_log)) {
            //提供 IG_Log::log()->{错误等级}([参数 ...]) 的使用方法
            if (is_null($message))
                return self::$_log;
            else
                self::$_log->log($message, $priority, $extras);
        } else {
            return new self();
        }
    }

    /**
     * 避免程序未执行 工厂模式,而导致 IG_Log::log 使用出错
     * @param $m
     * @param $p
     */
    public function __call($m, $p) {}

    /**
     * IG_Log
     * @param array|Zend_Config $config
     * @return Zend_Log
     */
    static public function factory($config = array())
    {
        self::$_log = Zend_Log::factory($config);

        //日志增加等级, 事件增加员工名
        self::$_log->registerErrorHandler()
                   ->addPriority('OPERATOR', self::OPERATOR);
        return self::$_log;
    }
}